package com.cqjtu.store.favorite.service.impl;

import com.cqjtu.store.common.PageVO;
import com.cqjtu.store.common.ServiceCode;
import com.cqjtu.store.common.ServiceException;
import com.cqjtu.store.favorite.mapper.FavoriteMapper;
import com.cqjtu.store.favorite.pojo.dto.FavoritePageDTO;
import com.cqjtu.store.favorite.pojo.entity.Favorite;
import com.cqjtu.store.favorite.service.FavoriteService;
import com.cqjtu.store.product.mapper.ProductMapper;
import com.cqjtu.store.product.pojo.entity.Product;
import com.cqjtu.store.product.pojo.vo.ProductVO;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional  //事务
public class FavoriteServiceImpl implements FavoriteService {
    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private  ProductMapper productMapper;

    /**
     * 查询收藏列表
     * @param favoritePageDTO   分页的参数
     * @param userLoginVO   收藏人
     * @return
     */
    @Override
    public PageVO<List<ProductVO>> getFavoriteList(FavoritePageDTO favoritePageDTO,
                                                   UserLoginVO userLoginVO) {
        //取出用户编号
        Integer userId = userLoginVO.getId();
        favoritePageDTO.setUserId(userId);
        List<Product> productList = favoriteMapper.getFavoriteList(favoritePageDTO);
        if (productList == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该用户没有收藏商品");
        }
        List<ProductVO> productVOList = new ArrayList<>();
        productList.forEach(product -> {
            ProductVO productVO = new ProductVO();
            productVO.setId(product.getId());
            productVO.setTitle(product.getTitle());
            productVO.setPrice(product.getPrice());
            productVO.setImage(product.getImage());
            productVOList.add(productVO);
        });
        // 获取这个用户的总收藏   商品数据量
        int count = favoriteMapper.count(userLoginVO.getId());
        PageVO<List<ProductVO>> pageVO = new PageVO<>(favoritePageDTO.getPageIndex(),favoritePageDTO.getPageSize(),
                count,productVOList);
        return pageVO;
    }

    @Override
    public void addFavorite(Integer productId, UserLoginVO userLoginVO) {
        // 判断商品编号是否合法
        if(productId == null){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"商品编号参数不能为空");
        }
        Product product = productMapper.getProductById(productId);
        if(product == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"收藏的商品不存在");
        }
        //执行到这里说明商品是存在的
        Favorite favorite = favoriteMapper.getFavorite(productId, userLoginVO.getId());
        if(favorite != null){
            //已经收藏
            throw new ServiceException(ServiceCode.ERR_EXISTS,"该商品已经被收藏");
        }
        favorite = new Favorite();
        favorite.setUserId(userLoginVO.getId());
        favorite.setProductId(productId);
        favorite.setCreatedUser(userLoginVO.getUsername());
        //添加收藏
        int reslt = favoriteMapper.insert(favorite);
        if(reslt != 1){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"收藏失败");
        }
    }

    @Override
    public void cancelFavorite(Integer productId, UserLoginVO userLoginVO) {
        // 判断商品编号是否合法
        if(productId == null){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"商品编号参数不能为空");
        }
        int reslut = favoriteMapper.delete(productId, userLoginVO.getId());
        if(reslut == 0){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"收藏的商品不存在");
        }
    }
}
